//
//  InfnMationCollectionViewController.m
//  Arts1
//
//  Created by apple on 15/8/29.
//  Copyright (c) 2015年 Arts1. All rights reserved.
//

#import "InfnMationCollectionViewController.h"
#import "InfoMationCollectionViewCell.h"

#import "InfoMactionModel.h"
#import "AllHeader.h"

//#define kInfornURL @"http://arts1.dlab.com.cn/app/myfeed"
#define kInfornURL @"http://arts1.cn/app/myfeed"


typedef NS_ENUM(NSInteger, RefreshType) {
    RefreshTypeNew,
    RefreshTypeMore
};
@interface InfnMationCollectionViewController ()<UICollectionViewDelegateFlowLayout>

@property (nonatomic, assign) RefreshType refreshType;  //  保存当前状态 上拉加载  下拉刷新
@property (nonatomic ,strong) NSMutableArray *dataArray;

@end

@implementation InfnMationCollectionViewController

static NSString * const reuseIdentifier = @"Cell";


- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.automaticallyAdjustsScrollViewInsets = NO;
    //设置控制器的背景颜色
    self.collectionView.backgroundColor = kBackgroundColor;
    //注册类
    [self.collectionView registerClass:[InfoMationCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self requestData];
    
    __weak InfnMationCollectionViewController *productVC = self;
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        _refreshType = RefreshTypeNew;
        [productVC requestData];
    }];
   
}
/**
 *  请求数据
 */
- (void)requestData
{
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"dataForKeyAndUid"];
    NSString *uid = dic[@"data"][@"uid"];
    NSString *key = dic[@"data"][@"key"];
    
    NSDictionary *pardic = [NSDictionary dictionary];
    
    if (uid && key)
    {
        pardic = @{@"uid":uid,
                   @"key":key};
    }
    
    
    [manage POST:kInfornURL parameters:pardic
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:(NSJSONReadingMutableContainers) error:nil];
             
             if (_refreshType == RefreshTypeNew) {
                 [self.dataArray removeAllObjects];
             }

             int status = [dic[@"status"] intValue];
             if (status == 1) {
                 NSArray *list = dic[@"list"];
                 for (NSDictionary *dict in list) {
                     InfoMactionModel *model = [[InfoMactionModel alloc] init];
                     [model setValuesForKeysWithDictionary:dict];
                     [self.dataArray addObject:model];
                 }
                 [self.collectionView reloadData];
                 [self.collectionView.defaultHeader endRefreshing];
             }
             
             // 请求成功的时候调用这个block
//             NSLog(@"数据请求--%@", dic);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             // 请求失败的时候调用调用这个block
             NSLog(@"请求失败");
         }];
}


#pragma mark <UICollectionViewDataSource>

//设置分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//设置每个分区多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
//重绘cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    InfoMationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    InfoMactionModel *model = _dataArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    InfoMactionModel *model = _dataArray[indexPath.row];
    if (_pushInfoView) {
        _pushInfoView(model);
    }
}
//设置上下左右的距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 5, 5, 5);
}
#pragma mark <UICollectionViewDelegate>


@end
